import { DataTypes } from 'sequelize'

export default (sequelize) => {
  const Region = sequelize.define('Region', {
    code: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    level: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    parentCode: DataTypes.STRING
  })

  Region.associate = (models) => {
    Region.hasMany(models.Organization, {
      foreignKey: 'regionCode',
      sourceKey: 'code'
    })
  }
  
  return Region
} 